package com.admission.web.admin;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Academy;
import com.admission.model.Major;
import com.admission.service.AcademyManager;
import com.admission.service.MajorManager;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;
@Namespace("/admin")
public class MajorAdminAction extends CrudActionSupport<Major>{

	private static final long serialVersionUID = 488185923443490311L;
	private String id;
	private String academyId;
	private Major entity;
	//private Page<Academy> page = new Page<Academy>(PageUtils.DEFAULT_PAGE_SIZE);
	private Page<Major> page = new Page<Major>(PageUtils.DEFAULT_PAGE_SIZE);
    private MajorManager majorManager;
    private AcademyManager academyManager;
    private List<Academy> academyList;
    private String ids;

	public Major getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		page=majorManager.getPageByFilter(page, filters);
		List<PropertyFilter> filters2 = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p=new PropertyFilter("EQS_state","1");
		filters2.add(p);
		academyList=academyManager.getListByFilter(filters2);
		return "list";
	}
	public String getAllAcademy() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p=new PropertyFilter("EQS_state","1");
		filters.add(p);
		academyList=academyManager.getListByFilter(filters);
    	return "post";
	}
	@Override
	public String input() throws Exception {
		entity=majorManager.getMajorById(id);
		return "detail";
	}
	@Override
	public String save() throws Exception {
		 try{
			    majorManager.save(entity, academyId);
				//this.addActionMessage("操作成功");
			} catch (Exception e) {
			   this.addActionMessage("操作失败，请重新操作");
		    }
		 return list();	
	}
	//级联
	public String majorlist() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p = new PropertyFilter("EQS_academy.id", academyId);
		filters.add(p);
		List<Major> majors = majorManager.getListByFilter(filters);
		JSONArray majorArray = new JSONArray();
		JSONObject jo = null;
		//设置需要被格式化为JSON字符串的内容.
		for(Major major : majors){
			jo = new JSONObject();
			jo.put("id", major.getId());
			jo.put("name", major.getName());
			majorArray.put(jo);
		}
		// 渲染返回结果.
		Struts2Utils.renderJson(majorArray.toString());
		return null;
	}


	@Override
	public String delete() throws Exception {
		try{
			majorManager.delete(id);
			 this.addActionMessage("操作成功");
		   } catch (Exception e) {
			// TODO: handle exception
			   this.addActionMessage("操作失败，请重新操作");
		   }  
		return list();
	}
	public String deleteMore() throws Exception {
		majorManager.deleteMore(ids);
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if(StringUtils.isEmpty(id)){
			entity = new Major();
		}else{
			entity = majorManager.getMajorById(id);
		}
		
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Major getEntity() {
		return entity;
	}

	public void setEntity(Major entity) {
		this.entity = entity;
	}
	public MajorManager getMajorManager() {
		return majorManager;
	}
    @Autowired
	public void setMajorManager(MajorManager majorManager) {
		this.majorManager = majorManager;
	}

	public String getAcademyId() {
		return academyId;
	}

	public void setAcademyId(String academyId) {
		this.academyId = academyId;
	}

	public AcademyManager getAcademyManager() {
		return academyManager;
	}
 @Autowired
	public void setAcademyManager(AcademyManager academyManager) {
		this.academyManager = academyManager;
	}

	public List<Academy> getAcademyList() {
		return academyList;
	}
	
	public void setAcademyList(List<Academy> academyList) {
		this.academyList = academyList;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Page<Major> getPage() {
		return page;
	}

	public void setPage(Page<Major> page) {
		this.page = page;
	}
 
   
}
